package com.hengxinguotong.zhihuichengjian.ui.material;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.MaterialListAdapter;
import com.hengxinguotong.zhihuichengjian.bean.MaterialBean;
import com.hengxinguotong.zhihuichengjian.bean.MaterialRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshBase;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/10.
 */

public class MaterialListActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.tv_back)
    HXTextView tvBack;
    @BindView(R.id.tv_title)
    HXTextView tvTitle;
    @BindView(R.id.tv_detail)
    HXTextView tvDetail;
    @BindView(R.id.material_lv)
    PullToRefreshListView materialLv;
    private int pageIndex = 1;
    private List<MaterialBean> materialBeanList;
    private MaterialListAdapter adapter;
    public static final int MODELTYPE = 1;
    public static final int MATERIALTYPE = 2;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_list);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", MATERIALTYPE);
        if(type == MATERIALTYPE){
            tvTitle.setText("材料列表");
        }else{
            tvTitle.setText("规格型号列表");
        }
        materialLv.setMode(PullToRefreshBase.Mode.BOTH);
        setListener();
        getMaterialList("加载中…");
    }

    @OnClick({R.id.tv_back, R.id.tv_detail})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_detail:
                if(adapter != null){
                    if(null != adapter.getSelect()){
                        Intent intent = getIntent();
                        switch (type){
                            case MATERIALTYPE:
                                intent.putExtra("material", adapter.getSelect());
                                setResult(1001, intent);
                                break;
                            case MODELTYPE:
                                intent.putExtra("model", adapter.getSelect());
                                setResult(1002, intent);
                                break;
                        }
                        finish();
                    }else{
                        showToast("请至少选择一项");
                    }
                }
                break;
        }
    }

    private void setListener(){
        materialLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelect(position - 1);
            }
        });

        materialLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                getMaterialList(null);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getMaterialList(null);
            }
        });
    }

    private void getMaterialList(String alert){
        RequestParams params = new RequestParams();
        if(type == MODELTYPE){
            String materialId = getIntent().getStringExtra("materialId");
            params.addBodyParameter("materialId", materialId);
        }else{
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("pageNum", pageIndex + "");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            params.addHeader("Content-type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String url = null;
        if(type == MODELTYPE){
            url = "/material/materialModelList/";
        }else{
            url = "/material/materialList/";
        }
        Utils.requestData(alert, this, url + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if(materialLv.isRefreshing()){
                    materialLv.onRefreshComplete();
                }
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    MaterialRes materialRes = gson.fromJson(json, MaterialRes.class);
                    if(null !=materialRes.getValue()){
                        process(materialRes.getValue());
                    }
                }
            }

            @Override
            public void onFailure(String json) {
                if(materialLv.isRefreshing()){
                    materialLv.onRefreshComplete();
                }
                showEmptyView(materialLv);
            }
        });
    }

    private void process(List<MaterialBean> datas){
        if(pageIndex == 1){
            if(datas.size() == 0){
                showEmptyView(materialLv);
            }else{
                dismissEmptyView();
            }
            materialBeanList = datas;
            adapter = new MaterialListAdapter(materialBeanList, this);
            materialLv.setAdapter(adapter);
            if(datas.size() == Utils.ONCECOUNT){
                materialLv.setMode(PullToRefreshBase.Mode.BOTH);
            }
        }else{
            materialBeanList.addAll(datas);
            adapter.notifyDataSetChanged();
        }

        if(datas.size() < Utils.ONCECOUNT){
            materialLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        }

        if (datas.size() != 0) {
            pageIndex++;
        }
    }
}
